import SchemaParser from '../SchemaParser';

export default {
    initCfgList(tmpNode) {
        let retList = [];
        const tmpTypeObj = tmpNode.nodeTypeObj;

        if (tmpNode.attachPoint && tmpTypeObj.forkType === 'anyOf') {
            const tmpName = tmpNode.nodeName.split('_')[1];
            const tmpKey = `$_${tmpName}_type`;
            const tmpValue = tmpNode.parentPoint[tmpKey];
            const propItemInst = tmpTypeObj.refInst.find(item => {
                return item.properties.name.const === tmpValue;
            });
            if (propItemInst) {
                retList = this.initCfgListTrue(tmpNode, propItemInst);
            }
        } else if (tmpNode.attachPoint && !tmpNode.forkArrayOuter) {
            retList = this.initCfgListTrue(tmpNode);
        }
        return retList;
    },

    initCfgListTrue(tmpNode, pInst) {
        let retList = [];
        const tmpTypeObj = tmpNode.nodeTypeObj;

        const propItemInst = pInst || tmpTypeObj.refInst;
        const paramSchemaFull = tmpTypeObj.curSchemaFull;
        const paramSchemaId = tmpTypeObj.curSchemaId;
        retList = SchemaParser.getCfgList(propItemInst, paramSchemaFull, paramSchemaId);

        // 初始化枚举项目的默认值
        retList.forEach(item => {
            if (item.formType === 'select' && item.selectList && item.selectList.length > 0) {
                if (tmpNode.attachPoint[item.modelKey] === undefined) {
                    tmpNode.attachPoint[item.modelKey] = item.selectList[0].id;
                }
            } else if (item.formType === 'label' && item.selectList && item.selectList.length > 0) {
                tmpNode.attachPoint[item.modelKey] = item.selectList[0].id;
            }
        });
        return retList;
    },
};